package com.example.shopsystem.control;

import com.example.shopsystem.aop.GetIdUtil;
import com.example.shopsystem.entity.*;
import com.example.shopsystem.entity.vo.OrderDetailVo;
import com.example.shopsystem.entity.vo.OrderVo;
import com.example.shopsystem.entity.dto.ReturnDto;
import com.example.shopsystem.service.BusinessServiceImpl;
import com.example.shopsystem.service.GoodServiceImpl;
import com.example.shopsystem.service.OrderServiceImpl;
import com.example.shopsystem.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/filter/user")
public class UserControl {

    @Autowired
    UserServiceImpl userServices;
    @Autowired
    OrderServiceImpl orderServices;
    @Autowired
    BusinessServiceImpl businessService;
    @Autowired
    GoodServiceImpl goodService;

    @PostMapping("/fillAddress")
    public Result<Object> fillAddress(@RequestBody User user) {//填写地址
        user.setId(GetIdUtil.getCurrentUser());
        if (user.getId() == null) {
            return Result.phoneNull(null);
        }
        if (!userServices.fillAddress(user))
            return Result.updateFail(null);
        return Result.ok(null);
    }

    @PostMapping("/ModifyInfo")
    public Result<Object> ModifyInfo(@RequestBody User user) {//填写支付密码
        user.setId(GetIdUtil.getCurrentUser());
        if (user.getId() == null) {
            return Result.phoneNull(null);
        }
        if (user.getMoneyPwd().length() < 6) {
            return Result.formatError(null);
        }
        if (!userServices.fillMoneyPwd(user))
            return Result.updateFail(null);
        return Result.ok(null);
    }

    @PostMapping("/ModifyUser")
    public Result<Object> ModifyUser(@RequestBody User user) {//修改信息
        user.setId(GetIdUtil.getCurrentUser());
        if (user.getId() == null) {
            return Result.phoneNull(null);
        }
        if (!userServices.ModifyInfo(user)) {
            return Result.updateFail(null);
        }
        return Result.ok(null);


    }

    @PostMapping("/withdrawal")
    public Result<Object> withdrawal(@RequestBody Map map) {//提现
        map.put("id", GetIdUtil.getCurrentUser());
        if (map.get("id") == null || map.get("moneyPwd") == null) {
            return Result.phoneNull(null);
        }
        User user = userServices.viewInfo(String.valueOf(map.get("id")));
        if (user == null) {
            return Result.queryFail(null);
        }
        if (user.getMoneyPwd() == null) {//验证密码是否为空
            return Result.phoneNull(null);
        }
        if (!user.getMoneyPwd().equals(String.valueOf(map.get("moneyPwd")))) {//验证密码
            return Result.phoneBad(null);
        }
        if (Double.valueOf(map.get("outMoney").toString()) >= user.getBalance()) {//验证余额
            return Result.mdFail(null);
        }
        if (!userServices.modifyMoney(user, Double.valueOf(map.get("outMoney").toString()), 0)) {
            return Result.Fail(null);
        }
        return Result.ok(user.getBalance());
    }

    @PostMapping("/viewItem")
    public Result<Object> viewItem(@RequestBody Map map) {//查看个人信息
        map.put("id", GetIdUtil.getCurrentUser());
        if (map.get("id") == null) {
            return Result.phoneNull(null);
        }
        User user = userServices.viewInfo(String.valueOf(map.get("id")));
        if (user == null) {
            return Result.queryFail(null);
        }

        return Result.ok(user);
    }

    @PostMapping("/viewShopCar")
    public Result<Object> viewShopCar(@RequestBody Map map) {//显示购物车
        map.put("id", GetIdUtil.getCurrentUser());
        if (map.get("id") == null) {
            return Result.phoneNull(null);
        }
        List<ShopCar> shopCar = userServices.viewShopCar(String.valueOf(map.get("id")));
        if (shopCar == null) {
            return Result.queryFail(null);
        }

        return Result.ok(shopCar);
    }

    @PostMapping("/viewOrder")
    public Result<Object> viewOrder(@RequestBody Map map) {//查看我的所有订单
        map.put("id", GetIdUtil.getCurrentUser());
        if (map.get("id") == null) {
            return Result.phoneNull(null);
        }
        List<OrderDetailVo> orderDetailVo = orderServices.ViewUserSales(String.valueOf(map.get("id")));
        if (orderDetailVo.isEmpty()) {
            return Result.queryFail(null);
        }
        return Result.ok(orderDetailVo);
    }

    @PostMapping("/viewOrderDetail")
    public Result<Object> viewOrderDetail(@RequestBody Map map) {//显示订单详情

        if (map.get("orderId") == null) {
            return Result.Fail(null);
        }
        List<OrderDeail> orderDeails = orderServices.queryOrderDeail(String.valueOf(map.get("orderId")));
        if (orderDeails == null) {
            return Result.queryFail(null);
        }
        return Result.ok(orderDeails);
    }

    @PostMapping("/updateShopCar")
    public Result<Object> updateShopCar(@RequestBody Map map) {//更新购物车
        map.put("id", GetIdUtil.getCurrentUser());
        if (map.get("id") == null || map.get("goodId") == null) {
            return Result.parameterFail(null);
        }
        Boolean b = userServices.updateShopCar(String.valueOf(map.get("id")), String.valueOf(map.get("goodId")), Integer.valueOf(map.get("goodNumber").toString()));
        if (!b) {
            return Result.Fail(null);
        }
        return Result.ok(null);
    }

    @PostMapping("/returnGoods")
    public Result<Object> returnGoods(@RequestBody ReturnDto returnDto) {//退款申请
        List<Return> goodList = returnDto.getGoodList();
//        for (Return r :
//                goodList) {
//            System.out.println(r.toString());
//        }
        ReturnGood returnGood1 = returnDto.getReturnGood();
        if (!userServices.returnGoods(goodList, returnGood1)) {
            return Result.Fail(null);
        }
        return Result.ok(null);

    }

    @PostMapping("/establishOrder")
    public Result<Object> establishOrder(@RequestBody Map map) {//创建订单
        map.put("id", GetIdUtil.getCurrentUser());
        if (String.valueOf(map.get("id")) == null || String.valueOf(map.get("busId")) == null) {
            return Result.parameterFail(null);
        }
        Order order = new Order();
        order.setUId(String.valueOf(map.get("id")));
        order.setBusId(String.valueOf(map.get("busId")));
        if (!orderServices.insertOrder(order)) {
            return Result.Fail(null);
        }
        return Result.ok(null);
    }

    @PostMapping("/orderStatus")
    public Result<Object> orderStatus(@RequestBody OrderVo orderVo) {//取消订单
        orderVo.getUser().setId(GetIdUtil.getCurrentUser());
        if (String.valueOf(orderVo.getOrder().getState()) == null) {
            return Result.parameterFail(null);
        }
        if (!orderServices.updateOrder(orderVo)) {
            return Result.Fail(null);
        }

        return Result.ok(null);
    }

    @PostMapping("/viewUserCost")
    public Result<Object> viewUserCost(@RequestBody Map map) {//查看账单
        map.put("id", GetIdUtil.getCurrentUser());
        if (map.get("id") == null) {
            return Result.phoneNull(null);
        }
        List<UserCost> listCost = userServices.viewUserCost(String.valueOf(map.get("id")));
        if (listCost.isEmpty()) {
            return Result.queryFail(null);
        }
        return Result.ok(listCost);
    }

    @PostMapping("/pickUp")
    public Result<Object> pickUp(@RequestBody Order order) {//取货
        if (order == null) {
            return Result.Fail(null);
        }
        order.setState(5);
        if (!orderServices.updateOrderState(order)) {
            return Result.Fail(null);
        }
        if (!businessService.busModifyCom(order)) {
            return Result.Fail(null);
        }
        return Result.ok(null);
    }


    @PostMapping("/queryGoods")
    public Result<Object> queryGoods(@RequestBody Good good) {//搜索商品
        if (String.valueOf(good.getId()) == null && good.getName() == null) {
            return Result.parameterFail(null);
        }
        if (good.getId() == 0) {
//            System.out.println("aaaaa");
//            System.out.println(good.getId());
            return Result.ok(goodService.queryGood(good.getName()));
        }
//        System.out.println(goodService.queryGood(good.getName()) + "1");
        return Result.ok(goodService.viewItem(String.valueOf(good.getId())));
    }
}
